layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;

    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 'full-150'
         ,url:"/admin/subject/list"
         ,cellMinWidth: 80
         ,cols:  [[
             {field:'id',width:60,title: 'ID', sort: true}
             ,{field:'title', width: 220, title: '标题'}
             ,{field:'readMount', width:90,title: '阅读数', align:'center'}
             ,{field:'createTime', width: 180   , title: '创建时间'}
             ,{field:'isShow',width:110,title: '是否展示',templet:'#showTool'}
             ,{field:'isCommented',width:110,title: '是否推荐',templet:'#recommentedTool'}
             ,{field:'',width:520,title: '操作',toolbar: '#tools'}
              ]]
         ,page:true
     });

    //监听指定开关
    form.on('switch(switchTest)', function(data){
        var id =  data.othis.parents('tr').find("td :first").text();
        var prams = {
            id:id,
            isShow:this.checked?true:false
        }
        $.ajax({
            type:"POST"
            ,url:"/admin/subject/offline"
            ,data:prams
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }
        });
    });

    //监听指定开关
    form.on('switch(switchTest2)', function(data){
        var id =  data.othis.parents('tr').find("td :first").text();
        var prams = {
            id:id,
            isCommented:this.checked?true:false
        }
        $.ajax({
            type:"POST"
            ,url:"/admin/subject/commented"
            ,data:prams
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }
        });
    });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'detail'){
             //查看项目详情
             var url = "/admin/subject/editView/"+data.id;
             $(this).attr("data-url",url);
             parent.addTab($(this))
         }else {
             //编辑项目
            layer.msg("操作错误！");
         }
     });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                title:$("#title").val()
            }
        })
    });;

    $("#add").on("click",function () {
        parent.addTab($(this));
    });

    $("#updateSiteMap").on("click",function () {
        $.ajax({
            type:"POST"
            ,url:"/admin/system/updateSiteMap"
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }

        });
    });
});

function toAdd() {
    window.open("/admin/subject/addView", "_blank");
}